package org.water.sys.web;

import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.water.base.entity.R;
import org.water.base.web.BaseController;
import org.water.common.bind.annotation.CurrentUser;
import org.water.common.bind.annotation.MethodDesc;
import org.water.common.bind.annotation.ResultFormat;
import org.water.common.param.AppSysParam;
import org.water.common.util.StringUtil;
import org.water.sys.entity.SysRole;
import org.water.sys.entity.SysRoleResource;
import org.water.sys.entity.SysUser;
import org.water.sys.service.RoleResourceService;
import org.water.sys.service.RoleService;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.NetUtil;
/**
 * 角色控件器
 * @author qzy
 *
 */
@Controller
@RequestMapping("/admin/role")
public class RoleController extends BaseController {
	
	static Logger  sflog = LogManager.getLogger("cn.sf.RoleManager.Log");
	/**
	 * 角色服务
	 */
	@Autowired
	private RoleService roleService;
	@Autowired
	private RoleResourceService roleResourceService;
	
	/**
	 * 角色主页
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping("/index")
	public String index(HttpServletRequest request,HttpServletResponse response)
	{
		
		return "admin/role/roleList";
	}
	
	/**
	 * 根据用户名模糊查询
	 * @param model
	 * @return
	 */
	@MethodDesc(name="角色管理",value="查看角色列表")
	@RequestMapping(value="/qry")
	@ResultFormat
	public Page<SysRole> findByRoleName(Model model,HttpServletRequest request,
			HttpServletResponse response,@RequestParam("name") String roleName,
			@RequestParam("pageIndex") int pageIndex,@RequestParam("pageSize") int pageSize)
	{
		Page<SysRole> roles=null;
		PageRequest pageRequest=new PageRequest(pageIndex, pageSize);
		roles=roleService.findByRoleName("%"+roleName+"%",pageRequest);
		return roles;
	}
	/**
	 * 根据用户名模糊查询
	 * @param model
	 * @return
	 */
	@RequestMapping(value="/one")
	@ResultFormat
	public SysRole findOne(@RequestParam("id") String roleId)
	{
		return roleService.findOne(roleId);
	}
	/**
	 * 保存
	 * @param request
	 * @param response
	 * @return
	 */
	@MethodDesc(name="角色管理",value="保存角色")
	@RequestMapping(value="/save")
	@ResultFormat
	public R save(HttpServletRequest request,HttpServletResponse response,@CurrentUser SysUser user)
	{
		R bean=getBaseBean(true, "");
		SysRole role=new SysRole();
		role.setId(request.getParameter("id")!=""?request.getParameter("id"):null);
		role.setRoleName(request.getParameter("roleName"));
		role.setRemark(request.getParameter("remark"));
		role.setCreateDate(new Date());
		role.setCreateUser(user.getId());
		roleService.save(role);
//		sflog.info("3\u00001\u0000" + DateUtil.date(System.currentTimeMillis()) + "\u0000" + user.getUserName() + "\u0000"+AppSysParam.getProp("sys.code")+"\u0000"+AppSysParam.getProp("sys.code") + "\u0000" 
//				+ NetUtil.getLocalhost().getHostAddress() + "\u0000001" + "\u0000" + request.getParameter("roleName") + "\u0000Success\u0000\r");
		return bean;
	}
	/**
	 * 删除
	 * @param id
	 * @return
	 */
	@MethodDesc(name="角色管理",value="删除角色")
	@RequestMapping(value="/del")
	@ResultFormat
	public R del(@RequestParam("id") String id,@CurrentUser SysUser user)
	{
		R bean=getBaseBean(true, "");
		if(!StringUtil.isEmpty(id))
		{
			if(true==roleService.haseRoleUseByUser(id))
			{
				bean.setSuccess(false);
				bean.setMsg("角色已经被用户使用，不能删除");
			}
			else if(true==roleService.haseRoleRelResource(id))
			{
				bean.setSuccess(false);
				bean.setMsg("角色已经关联资源，不能删除");
			}
			else
			{
				roleService.del(id);
			}
		}
		else
		{
			bean.setSuccess(false);
			bean.setMsg("id为空");
		}
		/**if(bean.getSuccess()==true)
		{
			sflog.info("3\u00001\u0000" + DateUtil.date(System.currentTimeMillis()) + "\u0000" + user.getUserName() + "\u0000"+AppSysParam.getProp("sys.code")+"\u0000"+AppSysParam.getProp("sys.code") + "\u0000" 
					+ NetUtil.getLocalhost().getHostAddress() + "\u0000033" + "\u0000" + id + "\u0000Success\u0000\r");
		}
		else
		{
			sflog.info("3\u00001\u0000" + DateUtil.date(System.currentTimeMillis()) + "\u0000" + user.getUserName() + "\u0000"+AppSysParam.getProp("sys.code")+"\u0000"+AppSysParam.getProp("sys.code") + "\u0000" 
					+ NetUtil.getLocalhost().getHostAddress() + "\u0000033" + "\u0000" + id + "\u0000Fail\u0000\r");
		}**/
		return bean;
	}
	/**
	 * 角色关联资源
	 * @param request
	 * @param response
	 * @return
	 */
	@CacheEvict(value={"userOwnResoucrce","userOwnResoucrceFun"},allEntries=true)
	@MethodDesc(name="角色管理",value="角色关联资源")
	@RequestMapping(value="/save/resource")
	public R saveRelResource(HttpServletRequest request,HttpServletResponse response,@CurrentUser SysUser user)
	{
		R bean=getBaseBean(true, "");
		String roleId=request.getParameter("roleId");
		String resoureIdStr=request.getParameter("resourceId");
		String[] resourceIds=resoureIdStr.split(",");
		roleService.relResource(roleId, resourceIds);
		//sflog.info("3\u00001\u0000" + DateUtil.date(System.currentTimeMillis()) + "\u0000" + user.getUserName() + "\u0000"+AppSysParam.getProp("sys.code")+"\u0000"+AppSysParam.getProp("sys.code") + "\u0000" 
		//					+ NetUtil.getLocalhost().getHostAddress() + "\u0000002" + "\u0000" + request.getParameter("roleId") + "\u0000Success\u0000\r");
		return bean;
	}
	/**
	 * 查询角色关联资源
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value="/rel/resource")
	@ResultFormat
	public List<SysRoleResource> relResource(@RequestParam("roleId") String roleId)
	{
		return roleResourceService.findByRoleId(roleId);
	}
}
